package com.rulin.asp.business.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rulin.asp.business.entity.Dictionary;
import com.rulin.asp.business.entity.Question;
import com.rulin.asp.business.log.entity.QuestionLog;
import com.rulin.asp.business.log.service.QuestionLogService;
import com.rulin.asp.business.service.DictionaryService;
import com.rulin.asp.business.service.QuestionService;
import com.rulin.asp.sso.entity.UserUtils;
import com.rulin.asp.util.Consists;

@Controller
@RequestMapping("/quesCollection")
public class QuesCollecitonController {

	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private DictionaryService dictionaryService;
	
	@Autowired
	private QuestionLogService questionLogService;
	
	/**
	 * 打开题目收集页面
	 * @param view
	 * @return
	 */
	@RequestMapping(value = "/openQuesCollectionPage.html", method = RequestMethod.GET)
	public ModelAndView openQuesCollectionPage(ModelAndView view){
		setDiv(view);
		
		Question question = new Question();
		String id = UUID.randomUUID().toString();
		question.setQuesId(id);
		question.setQuesDiffLevel(Consists.QUES_DIFF_LEVEL_1);
		question.setQuesType(Consists.QUES_TYPE_1);
		view.addObject("form",question);
		
		view.setViewName("/business/quescollection");
		return view;
	}
	
	/**
	 * 保存题目信息
	 * @param question
	 */
	@RequestMapping(value = "/saveQuestion.json", method = RequestMethod.POST)
	public void saveQuesition(Question question){
		int i = questionService.saveQuestion(question);
		
		//保存题目日志信息
		String operateType = "1";
		if(i == 0){
			operateType = "1";
		}else{
			operateType = "2";
		}
		QuestionLog log = new QuestionLog();
		log.setQuesId(question.getQuesId());
		log.setOperateType(operateType);
		log.setUserAccount(UserUtils.getUser().getAccount());
		log.setUserName(UserUtils.getUser().getUserName());
		questionLogService.addQuestionLog(log);
	}
	
	public void setDiv(ModelAndView model){
		model.addObject("quesTypes", dictionaryService.getByModel("ques_type_param"));
		model.addObject("quesDiffLevels", dictionaryService.getByModel("ques_diff_level"));
		
		model.addObject("questionSources", dictionaryService.getByModel("question_source"));
		model.addObject("quesBookClasses", dictionaryService.getByModel("ques_book_class"));
		model.addObject("quesBookClasses1", dictionaryService.getByModel("ques_book_class_1"));
		model.addObject("quesBookClasses2", dictionaryService.getByModel("ques_book_class_2"));
		model.addObject("quesBookClasses3", dictionaryService.getByModel("ques_book_class_3"));
		model.addObject("userTypes", dictionaryService.getByModel("user_type"));
		model.addObject("quesBookClasses4", dictionaryService.getByModel("ques_book_class_4"));
	}
	
	@RequestMapping(value = "/getDicsByParam.json", method = RequestMethod.GET)
	@ResponseBody
	public List<Dictionary> getDicsByParam(String model){
		return dictionaryService.getByModel(model);
	}
}
